﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Transactions;

namespace BOL
{
    [Serializable]
     public  class EmpresaBL
    {
        public int add(DAL.Empresa entity)
        {

            using (TransactionScope ts = new TransactionScope())
            {

        
                dbGestionDataContext _db =
                    new dbGestionDataContext(Parametros.strConnString);

                DAL.Empresa db = new DAL.Empresa();

              db.codigo=db.getCorrelativo(_db);
                db.descripcion=entity.descripcion;
                db.direccion=entity.direccion;
                db.razonSocial=entity.razonSocial;
                db.ruc = entity.ruc;
                db.activo = entity.activo;

                db.fechaCre=DateTime.Now;
                db.fechaMod = DateTime.Now;
                db.usuarioCre = entity.usuarioCre;
                db.logoForm = "~/Images/logo/grupoEOM.jpg";
                _db.Empresas.InsertOnSubmit(db);
                _db.SubmitChanges();

                ts.Complete();
                return db.idInternal;
            }
        }
        public bool update(DAL.Empresa entity)
        {
            bool result = false;
            using (TransactionScope ts = new TransactionScope())
            {
              
                dbGestionDataContext _db =
                    new dbGestionDataContext(Parametros.strConnString);

                DAL.Empresa dbObject;
                var q = from p in _db.Empresas
                        where p.idInternal == entity.idInternal
                        select p;

                dbObject = q.First();
                //int bl = _db.CompareObject<Empresa>(dbObject, entity);



                dbObject.descripcion=entity.descripcion;
                    dbObject.direccion=entity.direccion;
                    dbObject.razonSocial = entity.razonSocial;
                    dbObject.ruc= entity.ruc;
                    dbObject.activo= entity.activo;
                    dbObject.fechaMod=DateTime.Now;
                    dbObject.usuarioMod = entity.usuarioMod;

                _db.SubmitChanges();


                ts.Complete();
                result = true;
            }
            return result;
        }


        public bool delete(int idInsumo)
        {

            bool result = false;
            using (TransactionScope ts = new TransactionScope())
            {

                dbGestionDataContext _db;
                _db = new dbGestionDataContext(Parametros.strConnString);

                DAL.Empresa dbObject;


                var q = from p in _db.Empresas
                        where p.idInternal == idInsumo
                        select p;
                
                dbObject = q.First();
                _db.Empresas.DeleteOnSubmit(dbObject);
                _db.SubmitChanges();
                ts.Complete();
                result = true;
            }
            return result;

        }

    }
}
